package gui;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * Provides a form to edit a text field font size.
 * @author Miroslav Vozabal
 *
 */
public class FontControl extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel;
	private JTextField textField;
	private JLabel sizeLabel;
	private JButton setButton;
	private JButton plusButton;
	private JButton minusButton;
	private JTextArea form1TextArea;
	private JFrame thisFrame;

	/**
	 * Form constructor.
	 * @param form1TextArea
	 */
	public FontControl(JTextArea form1TextArea) {
		//Arguments saving
		this.thisFrame = this;
		this.form1TextArea = form1TextArea;
		
		initializateComponents();
		createActions();		
	}
	/**
	 * Initializes the form components.
	 */
	private void initializateComponents() {	
		//Creating new components objects
		mainPanel = new JPanel(new FlowLayout());
		textField = new JTextField(10);
		sizeLabel = new JLabel("Font size:");
		setButton = new JButton("OK");
		plusButton = new JButton("+");
		minusButton = new JButton("-");
		
		//configuration of this form
		this.getContentPane().add(mainPanel);
		this.setSize(150, 200);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		
		//add components to mainPanel
		mainPanel.add(sizeLabel);
		mainPanel.add(textField);
		mainPanel.add(minusButton);
		mainPanel.add(plusButton);
		mainPanel.add(setButton);
		
		//configuration of textField
		textField.setText(Integer.toString((form1TextArea.getFont().getSize())));
	}	
	
	/**
	 * Creates form actions.
	 */
	private void createActions() {
		//Action after clicking on button -
		minusButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				int temp = Integer.parseInt(textField.getText());
				temp--;
				textField.setText(Integer.toString(temp));
				updateFont();
			}
		});
		//Action after clicking on button +
		plusButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				int temp = Integer.parseInt(textField.getText());
				temp++;
				textField.setText(Integer.toString(temp));
				updateFont();				
			}
		});
		//Action after clicking on button OK
		setButton.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent arg0) {
				updateFont();			
				thisFrame.dispose();
			}
		});
	}
	
	/**
	 * Takes number of font size from text field.
	 * @return
	 */
	private float getFontSize() {		
		return (float) Integer.parseInt(textField.getText());
	}
	/**
	 * Updates font in the form1(parent) textArea.
	 */	
	private void updateFont() {
		Font font = form1TextArea.getFont().deriveFont(getFontSize());
		form1TextArea.setFont(font);
	}
	
}
